from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import xmlrpclib
server_quit = False
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
                            requestHandler=RequestHandler)
server.register_introspection_functions()

# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)

# Register a function under a different name
def adder_function(x,y):
    return x + y
server.register_function(adder_function, 'add')

# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'div').
class MyFuncs:
    def _dispatch(self, method, params):
        global  server_quit
        if method == 'quit':
            server_quit = True
            return 'quit?'
        elif method == 'get_results':
            print params
            s = xmlrpclib.dumps(params, methodname = method, allow_none = 1)
            print s
            print type(s)
            return 'get_results?'



    def div(self, x, y):
        return x // y

server.register_instance(MyFuncs())

# Run the server's main loop
#server.serve_forever()
while 1:
    server.handle_request()
    if server_quit:
        break
